/*
 * Mutable reference cells.
 */

Cell is package {

import Prelude;

type CellInstruction of %a is CellGet or CellSet(%a);

type Cell of %a is alias of ((CellInstruction of %a) => Maybe of %a);

makeCell has type (%a) => Cell of %a;
makeCell(a) is
	let {
		var x := a;  
	} in (function (i) is
			case i in {
				CellGet is Just(x);
				CellSet(n) is valof {
					x := n;
					valis Nothing
				}
			});

cellRef has type (Cell of %a) => %a;
cellRef(c) is case c(CellGet) in { Just(x) is x };

cellSet has type action(Cell of %a, %a);
cellSet(c, x) do {
	_ is c(CellSet(x));		
}

}